/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;
import DTO.User;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import DTO.CFields;
public class UserBUS extends IData<User>{

    @Override
    public User add(User item) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected void add(User item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<User> list, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public User Find(User item, List<User> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected Object OnSubmit(User item, int t, SubConfig config) {
        Object result = 0;
        String mysql ="";
        PreparedStatement cs;      
        try 
        {
            if(t == 0)
            {
                mysql = "insert into " + CFields.tb_users + "(" + CFields.user_name
                        + "," + CFields.password + "," + CFields.type_id + ","
                        + CFields.email + "," + CFields.phone + ","
                        + CFields.create_date + "," + CFields.islock + ","
                        + CFields.loginEnd + "," + CFields.isdelete
                        +  ") " 
                        + " values(?,?,?,?,?,?,?,?,?)";                
                cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);  
                cs.setString(1, item.getUser_name());
                cs.setString(2, item.getPassword());
                cs.setInt(3, item.getType_id());
                cs.setString(4, item.getEmail());
                cs.setString(5, item.getPhone());
                cs.setTimestamp(6,item.getCreate_date());
                cs.setBoolean(7, false);
                cs.setTimestamp(8,item.getCreate_date());
                cs.setBoolean(9, false);
                cs.executeUpdate();
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1);      
                }
            }
            else if(t == 1)
            {
                mysql = "update " + CFields.tb_users + " set "                        
                        + CFields.password + "=?, "
                        + CFields.type_id + "=?, "
                        + CFields.email + "=?, "
                        + CFields.phone + "=?"
                        + " where " 
                        + CFields.id + "=?";
                cs = config.connec.prepareStatement(mysql);                
                cs.setString(1, item.getPassword());
                cs.setInt(2, item.getType_id());
                cs.setString(3, item.getEmail());
                cs.setString(4, item.getPhone());                
                cs.setLong(5, item.getId());
                result = cs.executeUpdate();
            }
            else
            {
                mysql = "delete from " + CFields.tb_users 
                        + " where "
                        + CFields.id + "=?";
                cs = config.connec.prepareStatement(mysql);
                cs.setLong(1, item.getId());
                result = cs.executeUpdate();
            }
        } 
        catch (SQLException ex) 
        {
            return -2;
        }        
        return result;
    }

    @Override
    public Object insertOnSubmit(User item, SubConfig config) {
        return this.OnSubmit(item, 0, config);
    }

    @Override
    public Object updateOnSubmint(User item, SubConfig config) {
        return this.OnSubmit(item, 1, config);
    }
    
    @Override
    public Object deleteOnSubmint(User item, SubConfig config) {
        return this.OnSubmit(item, 2, config);
    }

    @Override
    protected User getItem(ResultSet rs) {
        User item = new User();
        try {
            item.setUser_name(rs.getString(CFields.user_name));
            item.setPassword(rs.getString(CFields.password));
            item.setType_id(rs.getInt(CFields.type_id));
            item.setEmail(rs.getString(CFields.email));
            item.setPhone(rs.getString(CFields.phone));
            item.setCreate_date(rs.getTimestamp(CFields.create_date));
            item.setIsdelete(rs.getBoolean(CFields.isdelete));
            item.setId(rs.getLong(CFields.id));
        } 
        catch (SQLException ex) 
        {
            Logger.getLogger(UserBUS.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public List<User> getAll(SubConfig config) {
        String mysql = "select * from " + CFields.tb_users;
        return this.getAll(mysql, config);
    }
    
    /*
    [10-01-2015]Binh: 
        - Thêm hàm login
        - Trả về User nếu username và password đúng, ngược lại trả về NULL
    */
    public User login(String username, String password, SubConfig config) 
    {
        String mysql = "";
        PreparedStatement cs;
        mysql = "select * from " + CFields.tb_users 
                + " where " 
                + CFields.user_name + "=?"
                + "and"
                + CFields.password + "=?";
        try 
        {
            cs = config.connec.prepareStatement(mysql);
            cs.setString(1, username);
            cs.setString(2, password);
        } 
        catch (SQLException ex) 
        {
            Logger.getLogger(UserBUS.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        List<User> listUser = this.getAll(mysql, config);
        User item = null;
        if (listUser.size() > 0) {
            item = (User) listUser.get(0);
        }
        return item;
    }
}
